package org.oschina.api.blog.catalog.service;

import java.util.List;
import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.blog.blog.BlogType;
import org.oschina.api.blog.blog.service.BlogService;
import org.oschina.api.blog.catalog.dao.BlogCatalogDAO;
import org.oschina.api.blog.catalog.model.BlogCatalog;
import org.oschina.api.blog.catalog.vo.BlogCatalogVo;
import org.oschina.api.index.service.IndexService;
import org.oschina.api.user.user.model.User;
import org.oschina.api.user.user.service.UserService;
import org.oschina.api.user.user.vo.UserVo;
import org.oschina.bean.Result;
import org.oschina.security.annotation.Require;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BlogCatalogService extends BaseService {

	@Autowired
	private BlogCatalogDAO catalogDAO;
	@Autowired
	private IndexService indexService;
	@Autowired
	private UserService userService;
	@Autowired
	private BlogService blogService;
	
	/**
	 * 返回当前用户的目录
	 * @return
	 */
	@Require
	public List<BlogCatalogVo> findByToken()
	{
		return _findVoByUser(userService.getVo(getToken()).getId());
	}
	
	/**
	 * 根据用户返回目录
	 * @param id
	 * @return
	 */
	public List<BlogCatalogVo> findByUser(int id)
	{
		List<BlogCatalogVo> result=_findVoByUser(id);
		for(BlogCatalogVo blogCatalogVo:result)
		{
			blogCatalogVo.setCreate_time(null);
			blogCatalogVo.setUser_id(null);
		}
		return result;
	}
	
	/**
	 * 创建
	 * @param catalog
	 * @return
	 */
	@Require(authorization=Module.MODULE_39)
	public Result create(BlogCatalog catalog)
	{
		return ResultUtil.getSuccess(_create(userService.getVo(getToken()),catalog));
	}
	
	/**
	 * 更新
	 * @param catalog
	 * @return
	 */
	@Require(authorization=Module.MODULE_39)
	public Result update(BlogCatalog catalog)
	{
		User user=userService.getAuthenticationInfo(getToken());
		BlogCatalogVo catalogVo=_findVoById(catalog.getId());
		if(!_isAuthor(user.getId(), catalogVo))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_update(catalog);
		return ResultUtil.getSuccess(catalog);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param target 把博文转移到的目录
	 * @return
	 */
	@Require(authorization=Module.MODULE_39)
	public Result delete(int id,int target)
	{
		if(id==target)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		User user=userService.getAuthenticationInfo(getToken());
		
		BlogCatalogVo catalog=_findVoById(id);
		if(catalog==null || !_isAuthor(user.getId(), catalog))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		BlogCatalogVo targetCatalog=_findVoById(target);
		if(targetCatalog==null || !_isAuthor(user.getId(), targetCatalog))
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_delete(id);
		blogService._updateCatalogByAuthorAndType(user.getId(), BlogType.SIMPLE, id, target);
		return ResultUtil.getSuccess(Message.DELETE_SUCCESS);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	private int _delete(int id)
	{
		return catalogDAO.delete(id);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public BlogCatalogVo _findVoById(int id)
	{
		return catalogDAO.findVoById(id);
	}
	
	/**
	 * 是否是作者
	 * @param user
	 * @param catalog
	 * @return
	 */
	public boolean _isAuthor(int user,BlogCatalogVo catalog)
	{
		return (user==catalog.getUser_id());
	}
	
	/**
	 * 根据用户返回
	 * @param user
	 * @return
	 */
	public List<BlogCatalogVo> _findVoByUser(int user)
	{
		return catalogDAO.findVoByUser(user);
	}
	
	/**
	 * 更新数量_加
	 * @param id
	 * @param count
	 */
	public void _updateCount_plus(BlogCatalogVo blogCatalogVo)
	{
		catalogDAO.updateCount(blogCatalogVo.getId(), 1);
	}
	
	/**
	 * 更新数量_减
	 * @param id
	 * @param count
	 */
	public void _updateCount_minus(BlogCatalogVo blogCatalogVo)
	{
		catalogDAO.updateCount(blogCatalogVo.getId(), -1);
	}
	
	/**
	 * 创建
	 * @param catalog
	 * @return
	 */
	private BlogCatalog _create(UserVo user,BlogCatalog catalog)
	{
		catalog.setId(_getIndex());
		catalog.setUser_id(user.getId());
		catalog.setCount(0);
		catalog.setCreate_time(DateUtil.now());
		return catalogDAO.create(catalog);
	}
	
	/**
	 * 更新
	 * @param catalog
	 * @return
	 */
	private int _update(BlogCatalog catalog)
	{
		return catalogDAO.update(catalog);
	}
	
	private int _getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_BLOG_CATALOG);
	}
}